<?php
/*
	分佣中心
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$this->session('footernav','');
if($_RQ['op']=='main'){
	if(!$_RQ['toagent']){
		$member=$this->get('member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	}
	if($member['is_agent'] || $_RQ['toagent']){
		$chmember=$this->model('model')->checkagent();
	}
	if($chmember['ischecklogin']){
		$return['ischecklogin']=$chmember['ischecklogin'];
	}else{
		$member=$chmember;
	}
	$member=$member?:array();
	$return['setting']=$this->plugin_setting('page_setting');
	$diytitle=array('tagent','tshop','tmyshop','tcenter','tbecome','balance_button','tcommission','tcommission1','commission_total','commission_1','balance_total','balance_1','commission_1_1','commission_0','balance_2','balance_3','balance_pay','tcommission_detail','torder','tdown','tmydown','area_title','commission_area');
	foreach($diytitle as $v){
		$return['setting'][$v]=$return['setting'][$v.'2']?:$return['setting'][$v];
	}
	if($return['pluginset']['levefromid']){
		$level=$this->getcolumn('level',array('id'=>$return['pluginset']['levefromid']),'level');
		$parameter=array(
				'params'=>array('uniacid'=>$_SESSION['uniacid'],'level <'=>$level),
				'orderby'=>'level desc',
		);
		$levels=$this->getall('level',$parameter['params'],'','',$parameter['orderby']);
		if($member['level_id']==intval($levels[0]['id'])){
			$return['showlevefrom']=true;
		}
	}
	$return['xieyi']=$this->plugin_setting('xieyi');
	$tagent=$this->getcolumn('level',array('id'=>$return['pluginset']['levefromid']),'title');
	$tagent=$tagent?:'上级';
	$return['tagent']=$tagent;
	set_html('title',$return['setting']['title']?:'分佣中心');
	$basic=model('core/member')->basic($_SESSION['uid']);
	$member=array_merge($member,$basic);
	if($member['fromuser']){
		$member['agent']=model('core/member')->basic($member['fromuser']);
	}else{
		$member['agent']=array('nickname'=>'总店');
	}
	if($member['level_id']){
		$member['level']=$this->getcolumn('level',array('id'=>$member['level_id']),'title');
	}else{
		$member['level']=$this->diylevelname?:'预先分销商';
	}
	if($member['level_id']){
		$return['level']=$this->get('level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$member['level_id']));
	}else{
		$return['level']=$this->plugin_setting('default_level');
	}
	$return['member']=$member;
	$data=array();
	$data['congfee']=$this->sum('commission',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'type'=>6),'money');
	$data['fee1']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'type'=>1),'money');
	$data['fee2']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'type'=>2),'money');
	$data['fee3']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'type'=>3),'money');
	$data['fee4']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'type'=>4),'money');
	$data['fee5']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'type'=>5),'money');
	$data['fee6']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'type'=>6),'money');
	//$data['lastredfee']=$this->sum('fanlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'lastfanfee');
	$data['balance_num']=pdo_count('core_member_cash',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$lastdaytime = $daytime-24*3600;
	$data['tuifeeinall']=$this->sum('commission',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'money');
	$data['tuifeeinday']=$this->sum('commission',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime),'money');
	$data['tuifeeoutall']=$this->sum('balance',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'money');
	$data['tuifeeoutday']=$this->sum('balance',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime),'money');
	$data['credit2inall']=pdo_getsum('core_credit_log',array('type'=>2,'is_add'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fee');
	$data['credit2inday']=pdo_getsum('core_credit_log',array('type'=>2,'is_add'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime),'fee');
	$data['credit2outall']=pdo_getsum('core_credit_log',array('type'=>2,'is_add'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fee');
	$data['credit2outday']=pdo_getsum('core_credit_log',array('type'=>2,'is_add'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime),'fee');
	$data['credit1inall']=pdo_getsum('core_credit_log',array('type'=>1,'is_add'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fee');
	$data['credit1inday']=pdo_getsum('core_credit_log',array('type'=>1,'is_add'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime),'fee');
	$data['credit1outall']=pdo_getsum('core_credit_log',array('type'=>1,'is_add'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fee');
	$data['credit1outday']=pdo_getsum('core_credit_log',array('type'=>1,'is_add'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime),'fee');
	$levels=array();
	$teammids=$this->model('member')->getteamids($_SESSION['uid']);
	$return['member']['team_num']=count($teammids);
	$return['member']['member_num']=pdo_count('jy_lianfeeshop_member',array('gei_mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	foreach($data as &$row){
		$row=price($row);
	}
	$return['data']=$data;
	//公告列表
	$pindex = max(1, intval($post['page']));
	$psize = 10;
	$total='';
	$notice_list = pdo_getslice('jy_weishop_notice',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array($pindex,20),$total,array('id','title','createtime'),'','id desc');
	if($notice_list){
		foreach($notice_list as &$notice){
			$notice['createtime']=date('Y-m-d h:i',$notice['createtime']);
		}
	}
	$return['notice_list']=$notice_list;
	$status0 = pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>0,'deleted'=>0,'userdeleted'=>0));
	$status1 = pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1,'deleted'=>0,'userdeleted'=>0));
	$status2 = pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>2,'deleted'=>0,'userdeleted'=>0));
	$status3 = pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>3,'iscomment'=>0,'deleted'=>0,'userdeleted'=>0));
	$cartnums= pdo_getsum('jy_weishop_member_cart',array('mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
	$return['statics']=array(
			'status0'=>$status0,
			'status1'=>$status1,
			'status2'=>$status2,
			'status3'=>$status3,
			'cartnums'=>$cartnums
	);
	$return['rank']=$this->plugin_setting('rank');
	$return['dayorderfee']=$this->model('model')->get_dayfeelog();
	//DIY
	$fuwus = array(
			array('title'=>'我的推荐奖','icon'=>'icon-jiangjiatongzhi','color'=>'#cc99ff','action'=>'jy_lianfeeshop/order/'),
			array('title'=>'我的复购奖','icon'=>'icon-dixiaofei','color'=>'#cc99ff','action'=>'jy_lianfeeshop/order//type=9'),
			array('title'=>'收益结算','icon'=>'icon-daifukuan1','color'=>'#cc99ff','action'=>'jy_lianfeeshop/commission/'),
			array('title'=>'结算明细','icon'=>'icon-dixiaofei','color'=>'#cc99ff','action'=>'jy_lianfeeshop/balance/'),
			array('title'=>'我的粉丝','icon'=>'icon-paihao0','color'=>'#cc99ff','action'=>'jy_lianfeeshop/down/main/'),
			array('title'=>'商城动态','icon'=>'icon-tuwenwenzhen','color'=>'#cc99ff','action'=>'jy_weishop/news.list/'),
			array('title'=>'修改密码','icon'=>'icon-password','color'=>'#cc99ff','action'=>'core/member.cpassword/'),
			array('title'=>'我的购物车','icon'=>'icon-gouwuche','color'=>'#cc99ff','action'=>'jy_weishop/member.cart/'),
			array('title'=>'收货地址','icon'=>'icon-ditu','color'=>'#cc99ff','action'=>'core/member.address/'),
	);
	$return['pluginset']['fuwus']=$return['pluginset']['fuwus']?:$fuwus;
	$my_days = array(
			array('title'=>'我的优惠券','icon'=>'icon-youhuiquan','color'=>'#cc99ff','action'=>'jy_weishop/coupon.myindex/'),
			array('title'=>'微信会员卡','icon'=>'icon-daifahuo','color'=>'#cc99ff','action'=>'core/member.wxcard/main/'),
			array('title'=>'余额提现','icon'=>'icon-dixiaofei','color'=>'#cc99ff','action'=>'core/member.tixian/'),
			array('title'=>'地址管理','icon'=>'icon-position','color'=>'#cc99ff','action'=>'core/member.address/'),
			array('title'=>'帮助中心','icon'=>'icon-help','color'=>'#cc99ff','action'=>'jy_weishop/news.list/'),
			array('title'=>'获得购物卡','icon'=>'icon-qiangdan','color'=>'#cc99ff','action'=>'jy_weishop/member.getcard/'),
			array('title'=>'我的购物卡','icon'=>'icon-pingzhengzhongxin','color'=>'#cc99ff','action'=>'jy_weishop/member.mycard/'),
			array('title'=>'我的关注','icon'=>'icon-woguanzhudepinpai','color'=>'#cc99ff','action'=>'jy_weishop/member.favorite/'),
			array('title'=>'我的足迹','icon'=>'icon-zuji','color'=>'#cc99ff','action'=>'jy_weishop/member.history/'),
	);
	$return['pluginset']['my_days']=$return['pluginset']['my_days']?:$my_days;
	$my_logs = array(
			array('title'=>'积分日志','icon'=>'icon-jifen','color'=>'#cc99ff','action'=>'core/member.credit_log/'),
			array('title'=>'余额日志','icon'=>'icon-jiangjiatongzhi','color'=>'#cc99ff','action'=>'core/member.credit_log/'),
			array('title'=>'提现日志','icon'=>'icon-trade','color'=>'#cc99ff','action'=>'core/member.cash_log/'),
			array('title'=>'红包日志','icon'=>'icon-ai-hongbao','color'=>'#cc99ff','action'=>'core/member.red_log/'),
	);
	$return['pluginset']['my_logs']=$return['pluginset']['my_logs']?:$my_logs;
	$my_memberbtn = array(
			array('title'=>'领取优惠券','icon'=>'icon-youhuiquan1','color'=>'#cc99ff','action'=>'jy_weishop/coupon.index/'),
			array('title'=>'领取微信会员卡','icon'=>'icon-huiyuanqia','color'=>'#cc99ff','action'=>'core/member.wxcard/main'),
			array('title'=>'积分排行','icon'=>'icon-paixing','color'=>'#cc99ff','action'=>'jy_weishop/member.rank/'),
	);
	$return['pluginset']['my_memberbtn']=$return['pluginset']['my_memberbtn']?:$my_memberbtn;
	$htmls=array(
		array('type'=>'data','num'=>12,'one_num'=>3,'title'=>'总额/数量','datas'=>array(
			array('title'=>'总分佣','num'=>2,'data'=>array($data['fee_3'],$data['fee_3num'])),
			array('title'=>'已结算','yuan'=>1,'num'=>2,'data'=>array($data['balance_total'],$data['balance_num'])),
			array('title'=>'待结算','yuan'=>1,'num'=>2,'data'=>array($data['commission_1'],$data['commission_1num'])),
			array('title'=>'团队人数','num'=>2,'data'=>array($allmember_num,$allmember_num)),
		)),
	);
	$return['shopset']=plugin('jy_weishop')->plugin_setting('shopset');
	$_SESSION['current_plugin']='jy_lianfeeshop';
	$return['orderlist_url']='jy_lianfeeshop/order.list/';
	exi($return);
}
if($_RQ['op']=='apply'){
	$apply=$this->get('apply',array('mid'=>$_SESSION['uid']));
	if($apply['status']==0 && $apply['id']){
		exi('您已申请，请等待审核！','error');
	}
		$post=get_postdata();
		if(!$post['agree']){
			exi('请先阅读协议！','error');
		}
		if(!$post['realname']){
			exi('请输入姓名！','error');
		}
		if(!$post['mobile']){
			exi('请输入联系电话！','error');
		}
		if($post['diyformfields']){
			foreach ($post['diyformfields'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
			}
		}
		$member_update=array(
			'realname'=>trim($post['realname']),
			'mobile'=>trim($post['mobile']),
			'diyformdata'=>ijson_encode($post['diyformfields']),
		);
		if($post['wechat']){
			$member_update['wechat']=trim($post['wechat']);
		}
		$this->model('member')->member_update($_SESSION['uid'],$member_update);	
		if($post['agent_id']){
			$agent_id=intval($post['agent_id']);
			if($agent_id){
				$this->model('member')->set_parent($_SESSION['uid'],$agent_id);
			}
		}
		$this->model('member')->apply_agent($_SESSION['uid']);
		exi('申请成功！请耐心等待管理员审核！','',custom_url('jy_cun9fan/recharge/',true));
}
?>